<template>
  <div class="passingRate">
    <el-row :gutter="20">
      <el-col :span="show ? rowObj.T1 : rowObj.F1" :xs="show ? rowObj.T2 : rowObj.F2">
        <workSiteLeft ref="workSiteLeft" :show="show" @showLeft="showLeft" @queryVal="queryVal" />
      </el-col>
      <el-col :span="show ? rowObj.T3 : rowObj.F3" :xs="show ? rowObj.T4 : rowObj.F4">
        <el-form :inline="true" :model="queryParams" size="mini">
          <el-col :span="24">
            <el-button style="margin-right: 6px" size="mini" :icon="show ? 'el-icon-s-fold' : 'el-icon-s-unfold'" circle
              @click="showLeft(show)"></el-button>
            <el-form-item label="拌合楼">
              <el-select style="width: 130px" v-model="queryParams.stationCode" clearable size="mini"
                placeholder="请选择拌合楼">
                <el-option v-for="item in cementListData" :key="item.id" :label="item.name"
                  :value="item.code"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="级配类型">
              <el-select style="width: 120px" v-model="queryParams.matchType" clearable size="mini" placeholder="请选择级配类型">
                <el-option v-for="item in matchTypeList" :key="item.id" :label="item.type" :value="item.type"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="配方编号">
              <el-select style="width: 120px" v-model="queryParams.matchCode" clearable size="mini" placeholder="请选择">
                <el-option v-for="item in matchCodeList" :key="item.value" :label="item.label"
                  :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="选择日期">
              <deatetime ref="deatetime" v-model="queryParams.timeRange">
              </deatetime>
            </el-form-item>
            <el-form-item label="筛孔">
              <el-select style="width: 120px" v-model="queryParams.sieveSize" clearable :popper-append-to-body="false">
                <el-option :label="0.075" :value="0.075"></el-option>
                <el-option :label="2.36" :value="2.36"></el-option>
                <el-option :label="4.75" :value="4.75"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="handleQuery()">查询</el-button>
            </el-form-item>
          </el-col>

        </el-form>
        <div class="tabsBox">
          <div v-for="(tabs, index) in tabList" :key="tabs.label"
            :class="['tabItem', { tabItemActive: index == activeName }]" @click="handleTabs(index)">{{ tabs.label }}</div>
        </div>

        <div v-if="activeName == '0'">
          <el-table :data="dataList" v-loading="loading" fit border size="mini" highlight-current-row stripe>
            <!-- <el-table-column prop="deptName" label="基地" align="center"></el-table-column> -->
            <el-table-column prop="stationName" label="拌合楼" align="center"></el-table-column>
            <el-table-column prop="dischargeTime" label="检测日期" align="center"></el-table-column>
            <el-table-column prop="value" label="当天测试结果" align="center"></el-table-column>
            <el-table-column prop="threeDaysValue" label="前3天平均" align="center"></el-table-column>
            <el-table-column prop="difference" label="前3天平均值的均差" align="center"></el-table-column>
          </el-table>
          <pagination class="fenye" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>
        <div v-else>
          <div id="myChar" style="width: 100%;height: 500px;" v-loading="chartLoading"></div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import {
  sysStationList,// 沥青拌合楼
  getInitMatchCode,// 配方编号
  asphaltMatchTypeList,//级配类型
  getLatestData,
  produceSievePassRate,//列表
} from "@/api/bitumenMixingStation/producedData/passingRate";
import deatetime from "@/components/Datetime";
import workSiteLeft from "@/views/components/workSiteLeft";

export default {
  name: "passingRate", //总量校核 
  data() {
    return {
      show: true,
      rowObj: {
        T1: 3,
        T2: 24,
        T3: 21,
        T4: 24,
        F1: 0,
        F2: 0,
        F3: 24,
        F4: 24,
      },
      myChar: null,
      chartLoading: true,
      tabList: [
        {
          value: '0',
          label: '报表'
        },
        {
          value: '1',
          label: '图形'
        }
      ],
      activeName: "0",
      submitting: true,
      visible: false,
      isAdd: true,
      dataList: [],
      loading: false,//表格加载
      loadingForm: false,//新增修改加载
      cementListData: [],//拌合楼下拉列表
      matchTypeList: [],//级配类型
      matchCodeList: [],
      projectIdList: [],
      matchNameList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      produceTypeOptions: [
        {
          value: '1',
          label: '进料加工'
        },
        {
          value: '0',
          label: '来料加工'
        }
      ],
    };
  },
  filters: {
    showMatchType(val) {
      if (val) {
        return val
      }
      return '全部'
    }
  },
  components: {
    deatetime,
    workSiteLeft
  },

  created() {
    this.getCementList();
    this.getAsphaltMatchTypeList()
    this.getInitMatchCodeData()
    this.init();
    this.getList();

  },
  methods: {
    showLeft(show) {
      this.show = !show;
    },
    queryVal(val) {
      this.queryParams.id = val.id;
      this.handleQuery();
    },
    async init() {
      await getLatestData().then((rs) => {
        if (!!rs.data) {
          this.queryParams = {
            stationCode: rs.data.stationCode,
            matchType: rs.data.matchType,
            matchCode: rs.data.matchCode
          };

        }
      });
    },

    getList() {
      this.loading = true;
      produceSievePassRate(this.queryParams).then((res) => {
        this.dataList = res.rows;
        this.total = res.total;
        this.loading = false;
        this.createChart(res.rows);
        this.chartLoading = false;
      });
    },

    /**
 * 创建图表
 */
    createChart(dataList) {
      if (null == this.myChar) {
        this.myChar = echarts.init(document.getElementById("myChar"));
      } else {
        this.myChar.clear();
      }
      if (!dataList || 0 == dataList.length) {
        return;
      }

      let lData = ['当天测试结果', '前3天平均'];
      let xData = [];
      let sData1 = [];
      let sData2 = [];
      for (let i = dataList.length - 1; i >= 0; i--) {
        sData1.push(dataList[i].value);
        sData2.push(dataList[i].threeDaysValue);
        xData.push(dataList[i].dischargeTime);
      }

      let sieveSize = this.searchParams.sieveSize ? this.searchParams.sieveSize : '0.075';
      let countInfo = sieveSize + '筛孔通过率，共' + dataList.length + '条';
      this.myChar.setOption({
        title: {
          text: '动态检测通过率',
          subtext: countInfo,
          subtextStyle: {
            fontSize: 14,
            color: '#666'
          }
        },
        legend: {
          data: lData
        },
        tooltip: {
          trigger: 'axis'
        },
        xAxis: {
          name: '日期',
          data: xData,
          boundaryGap: false,
          axisLine: { onZero: true },
          splitLine: { show: false },
          splitArea: { show: false },
          axisLabel: {
            rotate: 40
          }
        },
        yAxis: {
          name: '百分比(%)'
        },
        grid: {
          top: 100,
          bottom: 60,
          left: 60,
          right: 60
        },
        series: [
          {
            name: lData[0],
            type: 'line',
            symbol: 'none', //'circle',
            smooth: true,
            itemStyle: {
              normal: {
                color: '#409EFF',
                lineStyle: {
                  color: '#409EFF'
                }
              }
            },
            data: sData1
          },
          {
            name: lData[1],
            type: 'line',
            symbol: 'none', //'circle',
            smooth: true,
            itemStyle: {
              normal: {
                color: '#44CCF9',
                lineStyle: {
                  color: '#44CCF9'
                }
              }
            },
            data: sData2
          }
        ]
      });
    },

    getSearchParams() {
      const params = {};
      for (const key in this.queryParams) {
        if (key === "time") {
          const time = this.queryParams[key];
          if (time && time.length === 2) {
            params.startTime = time[0];
            params.endTime = time[1];
          }
        } else {
          params[key] = this.queryParams[key];
        }
      }
      return params;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams = this.getSearchParams();
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleTabs(index) {
      this.activeName = index
      this.getList()
    },
    // 配方编号
    getInitMatchCodeData() {
      getInitMatchCode().then((res) => {
        this.matchCodeList = res.data
      })
    },
    // 查询拌合楼下拉列表
    getCementList() {
      sysStationList({
        type: 'asphalt'
      }).then((res) => {
        this.cementListData = res.data || [];
      });
    },
    // 级配类型
    getAsphaltMatchTypeList() {
      asphaltMatchTypeList().then((res) => {
        this.matchTypeList = res.data;
      });
    },
  },
};
</script>
<style scoped lang="scss">
.passingRate {
  padding: 16px;

  .tabsBox {
    display: flex;
    width: 100%;
    border-bottom: 2px solid #dfe4ed;

    .tabItem {
      cursor: pointer;
      padding: 0 20px;
      height: 40px;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      line-height: 40px;
      display: inline-block;
      list-style: none;
      font-size: 14px;
      font-weight: 500;
      color: #303133;
    }

    .tabItemActive {
      transition: opacity 0.5s ease; // 设置过渡持续时间、速度曲线等参数
      color: #00508A;
      border-bottom: 2px solid #00508A;
    }
  }
}
</style>






